//
//  JWCBaseViewController.m
//  TableView封装
//
//  Created by Ju on 14-8-20.
//  Copyright (c) 2014年 dono. All rights reserved.
//

#import "JWCBaseViewController.h"
#import "JWCBaseCell.h"
#import "JWCBaseCellItem.h"

@interface JWCBaseViewController ()

@end

@implementation JWCBaseViewController

- (void)loadView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.view = tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _allGroup = [[NSMutableArray alloc] init];
}

#pragma mark tableview delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _allGroup.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    JWCSection *sectionGroup = _allGroup[section];
    return sectionGroup.items.count;
}

#pragma mark 每次cell进入视野时调用此方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    JWCBaseCell *cell = [NSClassFromString(_cellClassName) cellWithTableView:tableView reuseIdentifier:_cellClassName];
    
    JWCSection *sectionGroup = _allGroup[indexPath.section];
    JWCBaseCellItem *item = sectionGroup.items[indexPath.row];
    
    Class targetCell = [[item class] cellClass];
    
    JWCBaseCell *cell = [targetCell cellWithTableView:tableView reuseIdentifier:NSStringFromClass(targetCell)];
    
    cell.item = item;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWCSection *sectionGroup = _allGroup[indexPath.section];
    JWCBaseCellItem *item = sectionGroup.items[indexPath.row];
    if (item.operation) {
        item.operation();
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWCSection *sectionGroup = _allGroup[indexPath.section];
    
    return [sectionGroup.itemsHeight[indexPath.row] floatValue];
}



@end
